package cn.edu.neusoft.amicooldemo.model;

import java.util.List;

import cn.edu.neusoft.amicooldemo.bean.ArticleBean;
import cn.edu.neusoft.amicooldemo.bean.ProjectBean;
import cn.edu.neusoft.amicooldemo.bean.TcaseBean;
import cn.edu.neusoft.amicooldemo.bean.TwareBean;
import cn.edu.neusoft.amicooldemo.bean.VideoBean;
import cn.edu.neusoft.amicooldemo.iface.AmicoolListener;
import cn.edu.neusoft.amicooldemo.iface.FocusIface;
import cn.edu.neusoft.amicooldemo.iface.FocusListener;
import cn.edu.neusoft.amicooldemo.service.FocusService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Neusoft on 2016/12/21.
 */

public class FocusModel<T> extends BaseModel implements FocusIface<T> {
    private FocusService service;
    private static FocusModel model;


    public FocusModel(){
        super();
    }
    public static FocusModel getInstance(){
        if(model==null){
            model = new FocusModel();

        }
        return model;
    }


    @Override
    public void focus(String mod, int idolid, String sessionID, final FocusListener listener) {

        service = createService(FocusService.class,"scalar");
        Call<String> call = service.focus(mod,idolid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.body()==null)
                    listener.onFocusFail("0");
                else if(response.body().trim().equals("0"))
                    listener.onFocusResponse("1");
                else
                    listener.onFocusResponse("0");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFocusFail("fail:"+t.toString());
            }
        });

    }

    @Override
    public void unfocus(String mod, int id, String sessionID, final  FocusListener listener) {
        service = createService(FocusService.class);
        Call<String> call = service.unfocus(mod,id,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.body()==null)
                listener.onFocusFail("0");
                else if(response.body().trim().toString().equals("0"))
                    listener.onFocusResponse("3");
                else
                    listener.onFocusResponse("1");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFocusFail("fail:"+t.toString());
            }
        });
    }

    @Override
    public void exists(String mod, int idolid, String sessionID, final FocusListener listener) {
        service = createService(FocusService.class,"scalar");
        Call<String> call = service.exists(mod,idolid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.body()==null)
                    listener.onFocusFail("0");
                else if(response.body().trim().toString().equals("0"))
                    //已关注
                    listener.onFocusResponse("3");
                else
                    listener.onFocusResponse("1");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFocusFail("fail:"+t.toString());
            }
        });
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, int userid, AmicoolListener<T> listener) {
        service = createService(FocusService.class);

        switch (mod) {
            case "video":
                Call<List<VideoBean>> call = service.getVideoList(mod,page,sessionID,userid);
                call.enqueue(new CallbackFocus<VideoBean>(page, listener));
                break;
            case "article":
                Call<List<ArticleBean>> call2 = service.getArticleList(mod,page,sessionID,userid);
                call2.enqueue(new CallbackFocus<ArticleBean>(page, listener));
                break;
            case "tware":
                Call<List<TwareBean>> call3 = service.getTwareList(mod,page,sessionID,userid);
                call3.enqueue(new CallbackFocus<TwareBean>(page, listener));
                break;
            case "tcase":
                Call<List<TcaseBean>> call4 = service.getTcaseList(mod,page,sessionID,userid);
                call4.enqueue(new CallbackFocus<TcaseBean>(page, listener));
                break;
            case "project":
                Call<List<ProjectBean>> call5 = service.getProjectList(mod,page,sessionID,userid);
                call5.enqueue(new CallbackFocus<ProjectBean>(page, listener));
                break;


        }
    }

    @Override
    public void getResultBean(String mod, int id, String sessionID, AmicoolListener<T> listener) {

    }

    /*@Override
    public void getResultList(String mod, int page, String sessionID, FocusListListener<T> listener) {
        service = createService(FocusService.class,"gson");

        switch (mod) {
            case "video":
                Call<List<VideoBean>> call = service.getVideoList(mod,page,sessionID,);
                call.enqueue(new CallbackFocus<VideoBean>(page, listener));
                break;

        }
        Call<List<ProjectBean>> call = service.getResultList(mod,page,sessionID,userid);
        call.enqueue(new Callback<List<ProjectBean>>() {
            @Override
            public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                if((List<ProjectBean>) response.body()!=null){
                    if(page==1)
                        listener.onResponse((List<ProjectBean>) response.body());
                    else
                        listener.loadNextData((List<ProjectBean>) response.body());
                }
            }

            @Override
            public void onFailure(Call<List<ProjectBean>> call, Throwable t) {

            }
        });
    }

    @Override
    public void getResultBean(String mod, int id, String sessionID, FocusListListener<T> listener) {

    }*/
}
